//
//  CLReadLongPressViewController.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/31.
//

#import "CLReadLongPressViewController.h"
#import "CLReadLongPressView.h"
@interface CLReadLongPressViewController ()
/// 阅读视图
@property (nonatomic, strong) CLReadLongPressView *readView;

@end

@implementation CLReadLongPressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
// 初始化阅读视图
- (void)initReadView {
    
    // 是否为书籍首页
    if (self.recordModel.pageModel == nil || self.recordModel.pageModel.isHomePage) {
        
        [super initReadView];
        
    }else{
        
        // 阅读视图范围
        CGRect rect = CL_READ_VIEW_RECT();
        
        // 长按功能需要内容高度防止拖拽超出界限
        CLReadPageModel *pageModel = self.recordModel.pageModel;
        
        // 阅读视图
        CLReadLongPressView *readView = [[CLReadLongPressView alloc]init];
        self.readView = readView;
        readView.pageModel = pageModel;
        [self.view addSubview:readView];
        readView.frame = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width,  pageModel.contentSize.height);
    }
}

// MARK: 页面触摸拖拽处理

/// 触摸开始
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self drag:touches status:CLPanGesStatusBegin];
}

/// 触摸移动
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self drag:touches status:CLPanGesStatusChanged];
}

/// 触摸结束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self drag:touches status:CLPanGesStatusEnd];
}

/// 触摸取消
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self drag:touches status:CLPanGesStatusEnd];
}

/// 解析触摸事件
- (void)drag:(NSSet<UITouch *> *)touches status:(CLPanGesStatus)status {
    
    // 是否为书籍首页
    if (self.recordModel.pageModel == nil || self.recordModel.pageModel.isHomePage) { return; }
    
    if (self.readView.isOpenDrag) {
        
        CGPoint windowPoint = [touches.anyObject locationInView:self.view];
        if (!CGPointEqualToPoint(CGPointZero, windowPoint)) {
            CGPoint point = [self.view convertPoint:windowPoint toView:self.readView];
            [self.readView drag:status point:point
                    windowPoint:windowPoint];
        }
    }
}
@end
